<template>
  <div class="lanjian-content" style="height: 760px;overflow: auto">
    <div>

    </div>
    <a-form-model
      ref="ruleForm1"
      :labelCol="labelCol"
      :wrapperCol="wrapperCol"
      :rules="rules"
      class='formbox cusFormBox'
      :model="ruleForm"
    >
      <a-form-model-item
        class='formtabMax upload'
      >
        <a-upload
          :multiple="false"
          :file-list="fileList"
          :defaultFileList="ruleForm.fileList"
          :customRequest="customUpload"
          :before-upload="beforeFileUpload"
          :showUploadList="true"
          :remove="handleRemove"
          maxCount="1"
          @preview="downlocal"
          accept=".doc,.docx,.pdf,.xls,.xlsx,.jpg,.png,.jpeg,.txt,.et"
        ><span style="color: #000000d9;font-size: 18px;font-weight: 700; margin-right: 10px"><span
          style="color: #ff5500;font-size: 18px;">*</span>
            整改方案
          </span>
          <span><a-icon type="exclamation-circle" class="cio" /><span class="tip">仅支持doc、docx、pdf、xls、xlsx、jpg、png、jpeg、txt、et文件格式、文件大小不超过200M。</span></span>
          <a-button style="display:block;margin-top: 10px" >
            <span class='icon iconfont'>&#xe677;</span>
          </a-button>
        </a-upload>
      </a-form-model-item>
      <a-form-model-item
        label='编辑人'
        class='formtab'
        prop='editor'
      >
        <a-input
          v-model.trim='ruleForm.editor'
          placeholder='请输入编辑人'
          :maxLength='50'
        />
      </a-form-model-item>
      <a-form-model-item
        label='联系电话'
        class='formtab'
        prop='phone'
      >
        <a-input
          v-model.trim='ruleForm.phone'
          placeholder='请输入联系电话'
          :maxLength='50'
        />

      </a-form-model-item>
      <div class="save">
        <a-button type="primary" @click="save" v-show="m_status!='2'&&m_status!='5'">保存</a-button>
      </div>
    </a-form-model>

  </div>
</template>
<script>
import {
  getuploadFileInfo,
  addRectificationPlan,
  getRectificationPlanTotal,
  streamToBuffer,
  getJLID
} from '@/utils/JsonTools';
import UploadPlus from '@/components/UploadPlus/index.vue';
import {downLoadFunction, getLDType, getNowMilliSecond} from '@/utils/common';
import fs from 'fs';

export default {
  name: 'Total',
  components: { UploadPlus },
  data() {
    return {
      m_status:"",
      disble: false,
      jdjcs_ldyj: '',
      mulite: false,
      // 表单标题内容宽度设置
      labelCol: { span: 24 },
      wrapperCol: { span: 24 },
      ruleForm: {
        fileList: [],
        editor: '',
        phone:''
      },
      // 表单规则
      rules: {
        phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }],
        editor: [{ required: true, message: '请输入编辑人', trigger: 'blur' }],
      },
      fileList: [],
    };
  },
  mounted() {
    this.getInfos()
    this.getstatus()
  },
  methods: {
    downlocal(file){
      downLoadFunction(file.url,file.name)
    },

    // 上传验证
    beforeFileUpload(file) {
      if (file.name.substring(0, file.name.indexOf('.')).length > 200) {
        this.$message.error('上传文件名字不得超过200字');
        return false;
      }
      let isLt2M = file.size / 1024 / 1024;
      if (isLt2M > 200) {
        this.$message.error('只允许上传200M以下的文件');
        return false;
      }
      let fileExt = file.name.slice(file.name.lastIndexOf('.') + 1);
      let fileTypeArr = ['doc','docx','pdf','xls','xlsx','jpg','png','jpeg','txt','et'];
      if (fileTypeArr.indexOf(fileExt) === -1) {
        this.$message.error('非法的文件类型');
        return false;
      }
      return true;
    },
    handleRemove(){
     this.fileList=[]
    },
    customUpload(file) {
      // beforeFileUpload()返回为true，才会触发这个方法；
      this.handleUpload(file);
    },
    // 构造formData
    handleUpload(file) {
      // const formData = new FormData()
      // let timestamp = new Date().getTime()
      // formData.append('files', file)
      // formData.append('fileUid', file.uid)
      // formData.append('totalBytes', file.size)
      // formData.append('time', timestamp)
      // this.imagerAdd(formData)
      getuploadFileInfo(file.file).then(res => {
        file.onSuccess(res)
        // eslint-disable-next-line no-unused-expressions
        this.$nextTick(()=>{
           this.fileList =[]
          this.fileList.push(res)
        })

       // this.save()
      });
    },
    getInfos(){
      getRectificationPlanTotal().then(res=>{
        console.log("pp--",res)
        this.fileList=res===undefined||res.fileList===undefined?[]:res.fileList
        this.ruleForm=res
        if(res===undefined||res.f_status==="1"||res.f_status==="4"){
          this.disble=true
        }
      })
    },
    save () {
      this.$refs.ruleForm1.validate(valid => {
        // eslint-disable-next-line eqeqeq
        // alert(this.fileList.length)
        if (valid) {
          if(this.fileList.length==0){
            this.$message.warn('请上传相关文件')
            return
          }
          this.ruleForm.version=getNowMilliSecond()
          this.ruleForm.plan_status=0
          this.ruleForm.fileList=this.fileList
          addRectificationPlan(this.ruleForm).then(res => {
            if (res === 0) {
              this.$message.error('保存失败')
              return
            }
            this.$message.success('保存成功')
          })
        }
      })
    },
    downtmp(path,name){
      let fileb = fs.createReadStream(path);
      let fsname = name;
      console.log('fsname-----' + fsname);
      streamToBuffer(fileb).then(res => {
        let data = res;
        let contents = new Uint8Array(data.length);
        for (let i = 0; i < data.length; i++) {
          contents[i] = data[i];
        }
        // eslint-disable-next-line no-undef
        let expname = fsname;
        saveAs.saveAs(new Blob([contents], { type: 'application/zip' }), expname);
      });
    },
    getstatus(){
      const plantotal_dbson=JSON.parse(fs.readFileSync("dbson/RectificationPlanAllinfos.dbson","utf-8")==""?"[]":fs.readFileSync("dbson/RectificationPlanAllinfos.dbson","utf-8"))
      if(plantotal_dbson.length==0){
        this.m_status=="0"
        return;
      }
      let lsarr = plantotal_dbson.filter(function(value){
        //筛选条件
        return value.jlid==getJLID()
      })
      if(lsarr==undefined||lsarr.length==0){
        this.m_status=="0"
        return;
      }
      this.m_status=lsarr[0].f_status

    }
  },
};
</script>
<style scoped lang="less">
/deep/ .ant-form-item-label {
  width: 176px;
}

.save {
  margin: 40px auto;
}
.upload {
  position: relative;

  /deep/ .ant-upload-list {
    position: absolute;
    left: 50px;
    top: -13px
  }
}
.cio {
  width: 18px;
  height: 18px;
  margin-right: 4px;
  color: #73a8f6;
}

.tip {
  height: 26px;
  font-family: PingFangSC-Regular;
  font-size: 16px;
  font-weight: normal;
  font-stretch: normal;
  line-height: 26px;
  letter-spacing: 0px;
  color: #666666;
}

/deep/ .ant-input {
  width: 260px
}
.ant-form-item{
  margin-top: 50px;
}
</style>
